package com.xiaoxu.intranetweb.util.excel.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import com.baomidou.mybatisplus.extension.service.IService;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.CWeightSheets;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.excelEntity.WeightSheetsExcelEn;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.impl.CWeightSheetsServiceImpl;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;

import java.math.BigDecimal;
import java.util.List;

public class WeightSheetsListener implements ReadListener<WeightSheetsExcelEn> {
    public static final int BATCH_COUNT = 3000;

    private CWeightSheetsServiceImpl baseService;

    /**
     *临时存储
     */
    private List<CWeightSheets> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
    public WeightSheetsListener(CWeightSheetsServiceImpl baseService){
        this.baseService = baseService;
    }

    @Override
    public void invoke(WeightSheetsExcelEn data, AnalysisContext analysisContext) {
        CWeightSheets weightSheets = new CWeightSheets();
        BeanUtils.copyProperties(data,weightSheets);
        weightSheets.setWeishtMarketPrice(data.getWeishtPrice().subtract(data.getWeightSpecialPrice()));
        weightSheets.setWeishtMoney(data.getWeishtNetWght().multiply(data.getWeishtPrice()));
        weightSheets.setWeishtChiCo(",");
        weightSheets.setWeishtCoSupplyChi(",");
        weightSheets.setWeishtDeduction(new BigDecimal(1));
        weightSheets.setDataFrom("e9f6ed8e091611ec842700163e092c06");
        cachedDataList.add(weightSheets);
        if (cachedDataList.size() >= BATCH_COUNT) {
            saveData();
            // 存储完成清理 list
            cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        saveData();
    }


    private void saveData() {
        baseService.saveBatch(cachedDataList);
    }
}
